package com.looookme.shaozhuo.datasave;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;


/**
 * Created by Shaozhuo on 2015/9/11.
 */
public class Data_SharePreferences extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ll.setOrientation(LinearLayout.VERTICAL);
        setContentView(ll);

        Button saveBtn = new Button(this);
        saveBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        saveBtn.setText("Save Data");
        ll.addView(saveBtn);
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(Data_SharePreferences.this);
                SharedPreferences pref = getSharedPreferences("myPref", MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();

                //添加
                editor.putString("name", "张三");
                editor.putInt("age", 28);
                editor.putLong("time", System.currentTimeMillis());
                editor.putBoolean("defalut", false);
                editor.commit();    //操作完成必须提交
            }
        });

        Button cleanBtn = new Button(this);
        cleanBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        cleanBtn.setText("Clean Data");
        ll.addView(cleanBtn);
        cleanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences pref = getSharedPreferences("myPref", MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();

                //移除
                editor.remove("defalut");
                editor.commit();
            }
        });

        Button getBtn = new Button(this);
        getBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        getBtn.setText("Get Data");
        ll.addView(getBtn);
        getBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(Data_SharePreferences.this);
                SharedPreferences pref = getSharedPreferences("myPref", MODE_PRIVATE);
                Toast.makeText(Data_SharePreferences.this, pref.getAll().toString(), Toast.LENGTH_LONG).show();
            }
        });

    }
}
